package com.ymatou.smsproxy.api.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.smsproxy.api.parameter.MessagePackageBean;
import com.ymatou.smsproxy.api.parameter.SendMessagePackageBean;
import com.ymatou.smsproxy.api.service.SendMessagePackageCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.mongo.ApplicationConfigBean;
import com.ymttest.database.mongo.ApplicationConfigWapper;
import com.ymttest.database.mongo.ChannelConfigBean;
import com.ymttest.database.mongo.ChannelConfigWapper;
import com.ymttest.database.mongo.PhoneBlackListWapper;
import com.ymttest.database.mongo.SmsRecordBean;
import com.ymttest.database.mongo.SmsRecordWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;
/**
*短信包发送接口
*
*@author chenjiazhu
*/
@TestSuite
public class Ts_SendMessagePackage{
	private static SendMessagePackageBean sendmessagepackageBean;
	private static SendMessagePackageCall sendmessagepackageCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("短信包发送接口");
	}

	@Before
	public void caseUp() {
		sendmessagepackageBean=new SendMessagePackageBean();
		sendmessagepackageCall=new SendMessagePackageCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendmessagepackageCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessagePackage_001() {
		Logger.start(true,"正例-短信包发送单条");
		try {

			sendmessagepackageBean.setAppId("Test");
			
			List<MessagePackageBean> messagePackagelist = new ArrayList<MessagePackageBean>();
			
			MessagePackageBean messagePackageBean = new MessagePackageBean();
			messagePackageBean.setPhone("13000000001");
			messagePackageBean.setContext("自动化测试_短信包发送_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean);
			
			sendmessagepackageBean.setMessagePacketList(messagePackagelist);
			
			sendmessagepackageCall.setData(sendmessagepackageBean);
			sendmessagepackageCall.callService();			
			
			verifyReturn(false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessagePackage_002() {
		Logger.start(true,"正例-短信包单手机号发送多条");
		try {

			sendmessagepackageBean.setAppId("Test");
			
			List<MessagePackageBean> messagePackagelist = new ArrayList<MessagePackageBean>();
			
			MessagePackageBean messagePackageBean1 = new MessagePackageBean();
			messagePackageBean1.setPhone("13000000001");
			messagePackageBean1.setContext("自动化测试_短信包发送1_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean1);
			
			MessagePackageBean messagePackageBean2 = new MessagePackageBean();
			messagePackageBean2.setPhone("13000000001");
			messagePackageBean2.setContext("自动化测试_短信包发送2_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean2);
			
			MessagePackageBean messagePackageBean3= new MessagePackageBean();
			messagePackageBean3.setPhone("13000000001");
			messagePackageBean3.setContext("自动化测试_短信包发送3_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean3);
	
			sendmessagepackageBean.setMessagePacketList(messagePackagelist);
			
			sendmessagepackageCall.setData(sendmessagepackageBean);
			sendmessagepackageCall.callService();			
			
			verifyReturn(false);
				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessagePackage_003() {
		Logger.start(true,"正例-短信包多手机号发送多条");
		try {

			sendmessagepackageBean.setAppId("Test");
			
			List<MessagePackageBean> messagePackagelist = new ArrayList<MessagePackageBean>();
			
			MessagePackageBean messagePackageBean1 = new MessagePackageBean();
			messagePackageBean1.setPhone("13000000001");
			messagePackageBean1.setContext("自动化测试_短信包发送1_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean1);
			
			MessagePackageBean messagePackageBean2 = new MessagePackageBean();
			messagePackageBean2.setPhone("13000000002");
			messagePackageBean2.setContext("自动化测试_短信包发送2_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean2);
			
			MessagePackageBean messagePackageBean3= new MessagePackageBean();
			messagePackageBean3.setPhone("13000000003");
			messagePackageBean3.setContext("自动化测试_短信包发送3_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean3);
	
			sendmessagepackageBean.setMessagePacketList(messagePackagelist);
			
			sendmessagepackageCall.setData(sendmessagepackageBean);
			sendmessagepackageCall.callService();			
			
			verifyReturn(false);
				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessagePackage_004() {
		Logger.start(true,"反例-AppId空");
		try {
			
			List<MessagePackageBean> messagePackagelist = new ArrayList<MessagePackageBean>();
			
			MessagePackageBean messagePackageBean = new MessagePackageBean();
			messagePackageBean.setPhone("13000000001");
			messagePackageBean.setContext("自动化测试_短信包发送_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean);
			
			sendmessagepackageBean.setMessagePacketList(messagePackagelist);
			
			sendmessagepackageCall.setData(sendmessagepackageBean);
			sendmessagepackageCall.callService();			
				
			Logger.verifyEquals(ECode.smError, sendmessagepackageCall.getMessage(), "验证返回Message的报错信息");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessagePackage_008() {
		Logger.start(true,"反例-AppId不存在");
		try {

			sendmessagepackageBean.setAppId("112233");
			List<MessagePackageBean> messagePackagelist = new ArrayList<MessagePackageBean>();
			
			MessagePackageBean messagePackageBean = new MessagePackageBean();
			messagePackageBean.setPhone("13000000001");
			messagePackageBean.setContext("自动化测试_短信包发送_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean);
			
			sendmessagepackageBean.setMessagePacketList(messagePackagelist);
			
			sendmessagepackageCall.setData(sendmessagepackageBean);
			sendmessagepackageCall.callService();			
				
			Logger.verifyEquals(ECode.smError, sendmessagepackageCall.getMessage(), "验证返回Message的报错信息");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessagePackage_005() {
		Logger.start(true,"反例-MessagePacketList空");
		try {
			
			sendmessagepackageBean.setAppId("Test");		
			
			sendmessagepackageCall.setData(sendmessagepackageBean);
			sendmessagepackageCall.callService();			
				
			Logger.verifyEquals(null, sendmessagepackageCall.getReturnData(), "验证返回Message的报错信息");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessagePackage_006() {
		Logger.start(true,"反例-phone空");
		try {
			sendmessagepackageBean.setAppId("112233");
			List<MessagePackageBean> messagePackagelist = new ArrayList<MessagePackageBean>();
			
			MessagePackageBean messagePackageBean = new MessagePackageBean();

			messagePackageBean.setContext("自动化测试_短信包发送_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean);
			
			sendmessagepackageBean.setMessagePacketList(messagePackagelist);
			
			sendmessagepackageCall.setData(sendmessagepackageBean);
			sendmessagepackageCall.callService();			
				
			Logger.verifyEquals(ECode.smError, sendmessagepackageCall.getMessage(), "验证返回Message的报错信息");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessagePackage_007() {
		Logger.start(true,"反例-context空");
		try {
			sendmessagepackageBean.setAppId("112233");
			List<MessagePackageBean> messagePackagelist = new ArrayList<MessagePackageBean>();
			
			MessagePackageBean messagePackageBean = new MessagePackageBean();
			messagePackageBean.setPhone("13000000001");
			messagePackagelist.add(messagePackageBean);
			
			sendmessagepackageBean.setMessagePacketList(messagePackagelist);
			
			sendmessagepackageCall.setData(sendmessagepackageBean);
			sendmessagepackageCall.callService();			
				
			Logger.verifyEquals(ECode.smError, sendmessagepackageCall.getMessage(), "验证返回Message的报错信息");
	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessagePackage_009() {
		Logger.start(true,"正例-手机号在黑名单中,并且发送时间在有效时间内，中风控,该接口目前不在使用中");
		try {

			Date dNow = new Date(); // 当前时间
			Date dBefore = new Date();
			Date dAfter = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
			dBefore = calendar1.getTime(); // 得到前一天的时间

			Calendar calendar2 = Calendar.getInstance(); // 得到日历
			calendar2.setTime(dNow);// 把当前时间赋给日历
			calendar2.add(Calendar.DAY_OF_MONTH, 1); // 设置为后十天
			dAfter = calendar2.getTime(); // 得到前一天的时间

			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			
			new PhoneBlackListWapper().updateCollection("Test", "SendMessage", "10999999999", format.format(dBefore),format.format(dAfter));			
			
			sendmessagepackageBean.setAppId("Test");
			List<MessagePackageBean> messagePackagelist = new ArrayList<MessagePackageBean>();
			
			MessagePackageBean messagePackageBean1 = new MessagePackageBean();
			messagePackageBean1.setPhone("10999999999");
			messagePackageBean1.setContext("自动化测试_黑名单短信包发送1_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean1);
			
			MessagePackageBean messagePackageBean2 = new MessagePackageBean();
			messagePackageBean2.setPhone("10999999999");
			messagePackageBean2.setContext("自动化测试_黑名单短信包发送2_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean2);
	
			sendmessagepackageBean.setMessagePacketList(messagePackagelist);
			
			sendmessagepackageCall.setData(sendmessagepackageBean);
			sendmessagepackageCall.callService();			
				
			verifyReturn(true);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessagePackage_010() {
		Logger.start(true,"正例-手机号在黑名单中,发送时间不在在有效时间内，不中风控,该接口目前不在使用中");
		try {

			Date dNow = new Date(); // 当前时间
			Date dBefore = new Date();
			Date dAfter = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -2); // 设置为前一天
			dBefore = calendar1.getTime(); // 得到前一天的时间

			Calendar calendar2 = Calendar.getInstance(); // 得到日历
			calendar2.setTime(dNow);// 把当前时间赋给日历
			calendar2.add(Calendar.DAY_OF_MONTH, -1); // 设置为后十天
			dAfter = calendar2.getTime(); // 得到前一天的时间

			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			
			new PhoneBlackListWapper().updateCollection("Test", "SendMessage", "10999999999", format.format(dBefore),format.format(dAfter));			
			
			sendmessagepackageBean.setAppId("Test");
			List<MessagePackageBean> messagePackagelist = new ArrayList<MessagePackageBean>();
			
			MessagePackageBean messagePackageBean1 = new MessagePackageBean();
			messagePackageBean1.setPhone("10999999999");
			messagePackageBean1.setContext("自动化测试_黑名单短信包发送1_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean1);
			
			MessagePackageBean messagePackageBean2 = new MessagePackageBean();
			messagePackageBean2.setPhone("10999999999");
			messagePackageBean2.setContext("自动化测试_黑名单短信包发送2_"+TestDataManager.getRandomNum(10));
			messagePackagelist.add(messagePackageBean2);
	
			sendmessagepackageBean.setMessagePacketList(messagePackagelist);
			
			sendmessagepackageCall.setData(sendmessagepackageBean);
			sendmessagepackageCall.callService();			
				
			verifyReturn(false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn(boolean isInBlackList) throws Exception
	{
		Date now = new Date();
		String appid=sendmessagepackageBean.getAppId();
		List<MessagePackageBean> list=sendmessagepackageBean.getMessagePacketList();
		
		String phone="";
		String content = "[";
		List<String> phones=new ArrayList<String>();
		
		for(int i=0;i<list.size();i++)
		{	
			phone=list.get(i).getPhone();
			content +="{\\\"phone\\\":\\\""+phone+"\\\",\\\"context\\\":\\\""+list.get(i).getContext()+"\\\"}," ;
			phones.add(phone);
		}

		content = content.substring(0,content.length()-1);
		content +="]";
		
		Logger.comment("根据appid、content、phone到数据库表SmsRecord中查询相应记录来验证是否存入SmsRecord表");
		SmsRecordBean smsRecord = new SmsRecordWapper().getCollectionByAppIdContentPhones(appid, content, phones);

		Logger.verifyNotNull(smsRecord, "验证数据存入SmsRecord成功");
		
		ApplicationConfigBean applicationConfig = new ApplicationConfigWapper().getCollectionByIdAndAppId("Test-SendMessagePackage",appid);
		ChannelConfigBean channelConfig = new ChannelConfigWapper().getCollectionById(applicationConfig.getChannelId());
		
		if(isInBlackList)
		{
			Logger.verifyIsNull(smsRecord.getChannelId(), "验证中风控时数据库表SmsRecord中ChannelId字段为空");
			Logger.verifyIsNull(smsRecord.getChannelName(), "验证中风控时数据库表SmsRecord中ChannelName字段为空");
		}
		else {
			Logger.verifyEquals(applicationConfig.getChannelId(), smsRecord.getChannelId(), "验证数据库表SmsRecord中ChannelId字段与表ApplicationConfig中ChannelId一致");
			Logger.verifyEquals(channelConfig.getName(), smsRecord.getChannelName(), "验证数据库表SmsRecord中ChannelName字段与表ChannelConfig中Name一致");

		}
		
		Logger.verifyEquals("SendMessagePackage", smsRecord.getApiName(), "验证数据库表SmsRecord中getApiName字段为SendSmsVerifyCode");
		
		Logger.verifyEquals(TestDataManager.getLocalIp(), smsRecord.getClientIp(), "验证数据库表SmsRecord中ClientIp字段与发送端Ip一致");
		String host=EnvSetup.getSystemUrl("new_api_smsproxy").replace("http://", "").replace("/", "");
		Logger.verifyEquals(TestDataManager.getServerIP(host), smsRecord.getServerIp(), "验证数据库表SmsRecord中ServerIp字段与服务端Ip一致");

		Date createdTime=YMTDateUtil.getDateFromMongo(smsRecord.getCreatedTime().toString());
		Date sendedTime=YMTDateUtil.getDateFromMongo(smsRecord.getSendedTime().toString());
	
		Logger.verifyEquals(true, Math.abs(createdTime.getTime()-now.getTime())/(1000*60)<20, "验证数据库表SmsRecord中createdTime字段正确，值为："+smsRecord.getCreatedTime().toString()+",当前时间为"+now.toLocaleString());
		
		if(isInBlackList)
		{
			Logger.verifyEquals("1900-1-1 0:00:00", sendedTime.toLocaleString(), "验证数据库表SmsRecord中createdTime字段正确，值为："+smsRecord.getSendedTime().toString()+",当前时间为"+now.toLocaleString());

		}
		else {
			Logger.verifyEquals(true, Math.abs(sendedTime.getTime()-now.getTime())/(1000*60)<20, "验证数据库表SmsRecord中sendedTime字段正确，值为："+smsRecord.getSendedTime().toString()+",当前时间为"+now.toLocaleString());
		}

		if(isInBlackList)
		{
			Logger.verifyEquals(8, smsRecord.getMessageAction(), "验证数据库表SmsRecord中MessageAction字段值为8:手机号在黑名单中InBlackList");

		}
		else {
			Logger.verifyEquals(7, smsRecord.getMessageAction(), "验证数据库表SmsRecord中MessageAction字段值为7,非生产环境下，正常情况下是 发送消息到服务商失败SendMessageFail = 7");
		}
	}
}